package com.shoulder.boot.acl.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shoulder.boot.acl.dto.*;
import com.shoulder.boot.acl.entity.Role;
import com.shoulder.boot.acl.entity.User;
import com.shoulder.boot.acl.service.UserRoleService;
import com.shoulder.boot.acl.service.UserService;
import com.shoulder.boot.acl.vo.AdminLoginVo;
import com.shoulder.boot.acl.vo.TenantUserVo;
import com.shoulder.boot.acl.vo.UserVo;
import com.shoulder.core.annotation.Anonymous;
import com.shoulder.core.api.ApiResult;
import com.shoulder.core.api.ApiUser;
import com.shoulder.core.fetch.BaseIds;
import com.shoulder.core.page.PageSchema;
import com.shoulder.core.utils.TokenUtils;
import com.shoulder.log.annotation.LogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户信息管理
 */
@AllArgsConstructor
@RestController
@RequestMapping("/admin/api/v1/acl/user")
public class UserController {
    private final UserService userService;
    private final UserRoleService userRoleService;

    /**
     * 切换用户状态
     *
     * @param id id
     * @return ApiResult<Object>
     */
    @LogAnnotation(type = "切换用户状态")
    @PutMapping("/switch/{id}")
    public ApiResult<Object> switchStatus(@PathVariable String id) {
        userService.switchStatus(id);
        return ApiResult.ok();
    }

    /**
     * 获取用户角色
     * @param id 用户id
     * @return ApiResult<List<String>>
     */
    @LogAnnotation(type = "获取用户角色", record = false)
    @GetMapping("/roles/{id}")
    public ApiResult<List<String>> userRoles(@PathVariable String id) {
        List<Role> roles = userRoleService.findRolesByUserId(id);
        return ApiResult.ok(roles.stream().map(Role::getId).toList());
    }

    /**
     * 用户信息汇聚
     */
    @GetMapping("/info/gather")
    public ApiResult<TenantUserVo> infoGather() {
        TenantUserVo vo = userService.info();
        return ApiResult.ok(vo);
    }

    /**
     * 添加用户信息
     *
     * @author 596767880@qq.com
     */
    @LogAnnotation(type = "添加用户信息")
    @PostMapping
    public ApiResult<Object> add(@RequestBody @Validated UserAddDTO addDTO) {
        this.userService.add(addDTO);
        return ApiResult.ok();
    }

    /**
     * 修改用户信息
     *
     * @author 596767880@qq.com
     */
    @PutMapping
    @LogAnnotation(type = "修改用户信息")
    public ApiResult<Object> mod(@RequestBody @Validated UserModDTO modDTO) {
        this.userService.mod(modDTO);
        return ApiResult.ok();
    }

    /**
     * 查询用户信息-分页
     *
     * @author 596767880@qq.com
     */
    @PostMapping("/page")
    @LogAnnotation(type = "查询用户分页数据", record = false)
    public ApiResult<PageSchema<UserVo>> pageQuery(@RequestBody UserQueryDTO queryDTO) {
        Page<UserVo> page = this.userService.pageQuery(queryDTO);
        return ApiResult.ok(new PageSchema<>(page.getTotal(), page.getRecords()));
    }


    /**
     * 查询用户列表(不分页)
     *
     * @author 596767880@qq.com
     */
    @LogAnnotation(type = "查询用户列表", record = false)
    @PostMapping("/list")
    public ApiResult<List<UserVo>> list(@RequestBody UserQueryDTO queryDTO) {
        List<UserVo> list = this.userService.forList(queryDTO);
        return ApiResult.ok(list);
    }

    /**
     * 用户分配角色
     */
    @LogAnnotation(type = "用户分配角色")
    @PostMapping("/r2u")
    public ApiResult<Object> r2u(@RequestBody @Validated R2u r2u) {
        userService.r2u(r2u);
        return ApiResult.ok();
    }

    /**
     * 查询当前登录用户
     */
    @LogAnnotation(type = "查询当前登录用户", record = false)
    @GetMapping("/me")
    public ApiResult<ApiUser> me() {
        return ApiResult.ok(TokenUtils.parseUser());
    }

    /**
     * 用户登录
     */
    @LogAnnotation(type = "用户登录", isLogin = true)
    @PostMapping("/anon/login")
    @Anonymous
    public ApiResult<AdminLoginVo> login(@RequestBody @Validated AdminLoginDTO loginRequest) {
        return ApiResult.ok(this.userService.login(loginRequest));
    }


    /**
     * 刷新令牌
     */
    @LogAnnotation(type = "刷新token换取token")
    @PostMapping("/refresh")
    public ApiResult<AdminLoginVo> refresh(@RequestBody RefreshTokenDTO refreshTokenDTO) {
        return ApiResult.ok(this.userService.refresh(refreshTokenDTO));
    }

    /**
     * 批量删除用户信息
     *
     * @param baseIds 用户信息id集合
     * @return ApiResult<Object>
     * @author 596767880@qq.com
     */
    @DeleteMapping
    @LogAnnotation(type = "批量删除用户信息")
    public ApiResult<Object> delete(@RequestBody BaseIds<String> baseIds) {
        this.userService.batchDel(baseIds);
        return ApiResult.ok();
    }

    /**
     * 根据id获取用户信息
     *
     * @param id 用户信息id
     * @author 596767880@qq.com
     */
    @LogAnnotation(type = "根据id获取用户信息", record = false)
    @GetMapping("/{id}")
    public ApiResult<User> look(@PathVariable String id) {
        UserVo look = this.userService.look(id);
        return ApiResult.ok(look);
    }

}
